
package Test;

import java.awt.Color;
import java.io.File;

import junit.framework.TestCase;
import svgedit.gui.Frame;
import svgedit.svg.SVGCircleElement;
import svgedit.svg.SVGElement;
import svgedit.svg.SVGLength;
import svgedit.svg.SVGPaint;

/**
 *  Test case when insert an element and test when undo this execution
 *
 */
public class InsertElementTest extends TestCase {

	/**
	 *  Get Frame object
	 * */
	private static Frame frame;
	private static SVGCircleElement insertedCircle;
	private static int expectedChildrenNumber = 0;
	
	/**
	 *   Constructor for insertElement test
	 * */
	public InsertElementTest(String arg0) {
		super(arg0);
		frame = new Frame();
		File file = new File("src/testfile/test.svg");
        frame.openFile(file);
        SVGElement element = createElement();
        insertedCircle = (SVGCircleElement)element;
        expectedChildrenNumber = frame.getDocument().getRootGroup().getChildren().length;
	}
	
	/**
	 *  Create a new element for inserting
	 *  
	 *  @return insertedCircle to get the created item
	 * */
	public static SVGElement createElement(){
		// Create a circle element and set its attributes including cx, cy, cr, stroke, fill, stroke width
		insertedCircle = new SVGCircleElement(frame.getDocument());
		insertedCircle.getCX().newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBER, 20.0f);
	    insertedCircle.getCY().newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBER, 30.0f);
	    insertedCircle.getR().newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBER, 10.0f);
	    insertedCircle.getStrokeWidth().newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBER, 3.0f);
	    SVGPaint stroke = new SVGPaint();
	    stroke.setRGBColor(Color.red);
		insertedCircle.getStroke().setValueFromPaint(stroke);
		SVGPaint fill = new SVGPaint();
	    fill.setRGBColor(Color.blue);
		insertedCircle.getFill().setValueFromPaint(fill);
		
		return insertedCircle;
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();	
	}
	
	/**
	 *  Test unit when insert an element
	 * */
	public void testInsertElementTest(){
		InsertElement(insertedCircle);
	}
	
	/**
	 *  Test unit when undo the insert execution
	 * */
	public void testUndoInsertElementTest(){
		RemoveElement(insertedCircle);
	}
	
	/**
	 *  Remove the element from the root group
	 *  
	 *  @param element the inserted element
	 * */
	public void RemoveElement(SVGElement element){
		frame.getDocument().getRootGroup().removeChild(insertedCircle);
		expectedChildrenNumber -= 1;
		int actualChildrenNumber = frame.getDocument().getRootGroup().getChildren().length;
		assertEquals(expectedChildrenNumber,actualChildrenNumber);
	}
	
	/**
	 *  Add the element to the root group
	 *  
	 *  @param element the inserted element
	 * */
	public void InsertElement(SVGElement element){
		frame.getDocument().getRootGroup().appendChild(insertedCircle);
		expectedChildrenNumber += 1;
		int actualChildrenNumber = frame.getDocument().getRootGroup().getChildren().length;
		assertEquals(expectedChildrenNumber,actualChildrenNumber);
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}

}
